package cn.rkylin.oms.order.controller;

import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.rkylin.oms.order.vo.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.common.annotation.SystemControllerLog;
import cn.rkylin.oms.common.consts.YesNoConst;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;
import cn.rkylin.oms.common.utils.CodeUtil;
import cn.rkylin.oms.goods.service.IPrjSkuService;
import cn.rkylin.oms.goods.vo.PrjSkuVO;
import cn.rkylin.oms.order.dao.OrderDAOImpl;
import cn.rkylin.oms.order.domain.Order;
import cn.rkylin.oms.order.domain.OrderSku;
import cn.rkylin.oms.order.service.IOrderService;
import cn.rkylin.oms.warehouse.shiporder.vo.ShiporderVO;

@Controller
@RequestMapping("/order")
public class OrderController extends ApolloController {

    private static final Log logger = LogFactory.getLog(OrderController.class);
    private static final String PAGE_SELECT_STATEMENT = "pageSelectOrder";
    private static final String RESULT_SUCCESS_FLG = "success";
    private static final String RESULT_ERROR_MESSAGE = "errormessage";
    private static final String RESULT_ROW_COUNT = "rowcount";

    @Autowired
    public IOrderService orderService;

    @Autowired
    public IPrjSkuService prjSkuService;

    /**
     * 待审核
     *
     * @param quickSearch 快速查询条件
     * @param formJson    高级检索条件
     * @return 返回值JSON格式字符串
     * @throws Exception
     * @Param start 第几页
     * @Param length 每页多少行
     */
    @ResponseBody
    @RequestMapping(value = "/getWaitVerifyTable")
    public Map<String, Object> getWaitVerifyTable(String quickSearch, @RequestParam(required = false, defaultValue = "0") int start,
                                                  @RequestParam(required = false, defaultValue = "10") int length, String formJson) throws Exception {
        // 查询条件
        OrderVO param = new OrderVO();
        // 状态
        param.setOrderStatus("a");
        param.setIsCancel("n");

        // 下面代码放到private方法中时，会获取到上一个请求中的currentUser，所以先移到这里。
        Map<String, Object> returnMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, "当前用户失效");
            return returnMap;
        }
        String prjId = currentUser.getCurrentProject().getPrjId();
        param.setPrjId(prjId);
        return getDataList(quickSearch, start, length, formJson, param);
    }

    /**
     * 待分派
     *
     * @param quickSearch 快速查询条件
     * @param formJson    高级检索条件
     * @return 返回值JSON格式字符串
     * @throws Exception
     * @Param start 第几页
     * @Param length 每页多少行
     */
    @ResponseBody
    @RequestMapping(value = "/getWaitAssignTable")
    public Map<String, Object> getWaitAssignTable(String quickSearch, @RequestParam(required = false, defaultValue = "0") int start,
                                                  @RequestParam(required = false, defaultValue = "10") int length, String formJson) throws Exception {
        // 查询条件
        OrderVO param = new OrderVO();
        // 状态
        param.setOrderStatus("i");
        // param.setIsOrderError("n");
        // param.setIsGoodsError("n");
        // param.setIsLgstError("n");
        // param.setIsStockError("n");
        param.setIsCancel("n");

        // 下面代码放到private方法中时，会获取到上一个请求中的currentUser，所以先移到这里。
        Map<String, Object> returnMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, "当前用户失效");
            return returnMap;
        }
        String prjId = currentUser.getCurrentProject().getPrjId();
        param.setPrjId(prjId);
        return getDataList(quickSearch, start, length, formJson, param);
    }

    /**
     * 发货中
     *
     * @param quickSearch 快速查询条件
     * @param formJson    高级检索条件
     * @return 返回值JSON格式字符串
     * @throws Exception
     * @Param start 第几页
     * @Param length 每页多少行
     */
    @ResponseBody
    @RequestMapping(value = "/getShippingTable")
    public Map<String, Object> getShippingTable(String quickSearch, @RequestParam(required = false, defaultValue = "0") int start,
                                                @RequestParam(required = false, defaultValue = "10") int length, String formJson) throws Exception {
        // 查询条件
        OrderVO param = new OrderVO();
        // 状态
        param.setOrderStatus("k");
        // param.setIsOrderError("n");
        // param.setIsGoodsError("n");
        // param.setIsLgstError("n");
        // param.setIsStockError("n");
        param.setIsCancel("n");

        // 下面代码放到private方法中时，会获取到上一个请求中的currentUser，所以先移到这里。
        Map<String, Object> returnMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, "当前用户失效");
            return returnMap;
        }
        String prjId = currentUser.getCurrentProject().getPrjId();
        param.setPrjId(prjId);
        return getDataList(quickSearch, start, length, formJson, param);
    }

    /**
     * 已发货
     *
     * @param quickSearch 快速查询条件
     * @param formJson    高级检索条件
     * @return 返回值JSON格式字符串
     * @throws Exception
     * @Param start 第几页
     * @Param length 每页多少行
     */
    @ResponseBody
    @RequestMapping(value = "/getShippedTable")
    public Map<String, Object> getShippedTable(String quickSearch, @RequestParam(required = false, defaultValue = "0") int start,
                                               @RequestParam(required = false, defaultValue = "10") int length, String formJson) throws Exception {
        // 查询条件
        OrderVO param = new OrderVO();
        // 状态
        param.setOrderStatus("r");
        // param.setIsOrderError("n");
        // param.setIsGoodsError("n");
        // param.setIsLgstError("n");
        // param.setIsStockError("n");
        param.setIsCancel("n");

        // 下面代码放到private方法中时，会获取到上一个请求中的currentUser，所以先移到这里。
        Map<String, Object> returnMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, "当前用户失效");
            return returnMap;
        }
        String prjId = currentUser.getCurrentProject().getPrjId();
        param.setPrjId(prjId);
        return getDataList(quickSearch, start, length, formJson, param);
    }

    /**
     * 已完成
     *
     * @param quickSearch 快速查询条件
     * @param formJson    高级检索条件
     * @return 返回值JSON格式字符串
     * @throws Exception
     * @Param start 第几页
     * @Param length 每页多少行
     */
    @ResponseBody
    @RequestMapping(value = "/getCompleteTable")
    public Map<String, Object> getCompleteTable(String quickSearch, @RequestParam(required = false, defaultValue = "0") int start,
                                                @RequestParam(required = false, defaultValue = "10") int length, String formJson) throws Exception {
        // 查询条件
        OrderVO param = new OrderVO();
        // 状态
        param.setOrderStatus("z");
        // param.setIsOrderError("n");
        // param.setIsGoodsError("n");
        // param.setIsLgstError("n");
        // param.setIsStockError("n");
        param.setIsCancel("n");

        // 下面代码放到private方法中时，会获取到上一个请求中的currentUser，所以先移到这里。
        Map<String, Object> returnMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, "当前用户失效");
            return returnMap;
        }
        String prjId = currentUser.getCurrentProject().getPrjId();
        param.setPrjId(prjId);
        return getDataList(quickSearch, start, length, formJson, param);
    }

    /**
     * 已废弃
     *
     * @param quickSearch 快速查询条件
     * @param formJson    高级检索条件
     * @return 返回值JSON格式字符串
     * @throws Exception
     * @Param start 第几页
     * @Param length 每页多少行
     */
    @ResponseBody
    @RequestMapping(value = "/getCancelTable")
    public Map<String, Object> getCancelTable(String quickSearch, @RequestParam(required = false, defaultValue = "0") int start,
                                              @RequestParam(required = false, defaultValue = "10") int length, String formJson) throws Exception {
        // 查询条件
        OrderVO param = new OrderVO();
        // 状态
        param.setIsCancel("y");

        // 下面代码放到private方法中时，会获取到上一个请求中的currentUser，所以先移到这里。
        Map<String, Object> returnMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, "当前用户失效");
            return returnMap;
        }
        String prjId = currentUser.getCurrentProject().getPrjId();
        param.setPrjId(prjId);

        return getDataList(quickSearch, start, length, formJson, param);
    }

    /**
     * 根据不同的需求返回列表数据
     *
     * @param quickSearch
     * @param start
     * @param length
     * @param formJson
     * @return
     */
    private Map<String, Object> getDataList(String quickSearch, int start, int length, String formJson, OrderVO param) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            // 处理分页
            if (length == -1) {
                length = Integer.MAX_VALUE;
            }
            int page = start / length + 1;

            // 快速查询
            quickSearch = URLDecoder.decode(quickSearch, "UTF-8");
            quickSearch = quickSearch.trim();
            if (StringUtils.isNotBlank(quickSearch)) {
                param.setSearchCondition(quickSearch);
            }

            // 高级查询检索条件
            formJson = URLDecoder.decode(formJson, "UTF-8");
            // json转换成为ＶＯ
            FormVO formVO = JSONObject.parseObject(formJson, FormVO.class);
            if (formVO != null) {
                // 标记
                if (StringUtils.isNotBlank(formVO.getStamp())) {
                    param.setStamp(formVO.getStamp());
                }

                // 售后
                if (StringUtils.isNotBlank(formVO.getCustomerService()) && "1".equals(formVO.getCustomerService())) {
                    param.setEvent("1");
                }
                if (StringUtils.isNotBlank(formVO.getCustomerService()) && "0".equals(formVO.getCustomerService())) {
                    param.setEvent("0");
                }
                // 合并
                if (StringUtils.isNotBlank(formVO.getCombStus())) {
                    param.setCombStus(formVO.getCombStus());
                }

                // 验视？
                if (StringUtils.isNotBlank(formVO.getHasInspected())) {
                    param.setHasInspected(formVO.getHasInspected());
                }
                // 平台交易号
                if (StringUtils.isNotBlank(formVO.getTradeCode())) {
                    param.setTradeCode(formVO.getTradeCode());
                }

                // 订单编号
                if (StringUtils.isNotBlank(formVO.getOrderCode())) {
                    param.setOrderCode(formVO.getOrderCode());
                }

                // 店铺
                if (StringUtils.isNotBlank(formVO.getShopId())) {
                    param.setShopId(formVO.getShopId());
                }

                // 来源
                if (StringUtils.isNotBlank(formVO.getShopType())) {
                    param.setShopType(formVO.getShopType());
                }

                // 拍单时间 开始日期
                if (StringUtils.isNotBlank(formVO.getOrderTimeS())) {
                    param.setOrderTimeS(formVO.getOrderTimeS());
                }

                // 付款时间 开始日期
                if (StringUtils.isNotBlank(formVO.getPaidDateS())) {
                    param.setPaidDateS(formVO.getPaidDateS());
                }

                // 发货时间 开始日期
                if (StringUtils.isNotBlank(formVO.getShipTimeS())) {
                    param.setShipTimeS(formVO.getShipTimeS());
                }

                // 完成时间 开始日期
                if (StringUtils.isNotBlank(formVO.getFinishTimeS())) {
                    param.setFinishTimeS(formVO.getFinishTimeS());
                }

                // 拍单时间 截止日期
                if (StringUtils.isNotBlank(formVO.getOrderTimeE())) {
                    param.setOrderTimeE(formVO.getOrderTimeE());
                }

                // 付款时间 截止日期
                if (StringUtils.isNotBlank(formVO.getPaidDateE())) {
                    param.setPaidDateE(formVO.getPaidDateE());
                }

                // 发货时间 截止日期
                if (StringUtils.isNotBlank(formVO.getShipTimeE())) {
                    param.setShipTimeE(formVO.getShipTimeE());
                }

                // 完成时间 截止日期
                if (StringUtils.isNotBlank(formVO.getFinishTimeE())) {
                    param.setFinishTimeE(formVO.getFinishTimeE());
                }

                // 规格商家编码
                if (StringUtils.isNotBlank(formVO.getSkuCode())) {
                    param.setSkuCode(formVO.getSkuCode());
                }

                // 顾客ID
                if (StringUtils.isNotBlank(formVO.getBuyerNick())) {
                    param.setBuyerNick(formVO.getBuyerNick());
                }

                // 平台商品编码
                if (StringUtils.isNotBlank(formVO.getEcItemCode())) {
                    param.setEcItemCode(formVO.getEcItemCode());
                }

                // 平台规格编码
                if (StringUtils.isNotBlank(formVO.getEcSkuCode())) {
                    param.setEcSkuCode(formVO.getEcSkuCode());
                }

                // 问题单类型
                if (StringUtils.isNotBlank(formVO.getErrorType())) {
                    if (formVO.getErrorType().contains("isOrderError")) {
                        param.setIsOrderError("y");
                    }

                    if (formVO.getErrorType().contains("isGoodsError")) {
                        param.setIsGoodsError("y");
                    }

                    if (formVO.getErrorType().contains("isLgstError")) {
                        param.setIsLgstError("y");
                    }

                    if (formVO.getErrorType().contains("isStockError")) {
                        param.setIsStockError("y");
                    }
                }
            }

            // 处理转义的字段
            Map<String, String> replaceFieldsMap = new HashMap<String, String>();
            replaceFieldsMap.put("status", "order.order_status");
            // replaceFieldsMap.put("shopName", "o.shop_name");

            // 排序语句生成
            String orderStatement = getOrderString(OrderDAOImpl.class.getName(), PAGE_SELECT_STATEMENT, replaceFieldsMap);
            if (StringUtils.isNotEmpty(orderStatement)) {
                param.setOrderBy(orderStatement);
            }

            // 获取分页数据
            OrderVO vo = new OrderVO();
            PageInfo<OrderVO> orderVOList = orderService.findByWhere(page, length, param);

            // 列表中最后项，下拉按钮往上翻
            if (orderVOList != null && orderVOList.getList().size() > 4) {
                orderVOList.getList().get(orderVOList.getList().size() - 1).setReversePosition("dropup");
                if (orderVOList.getList().size() > 1) {
                    orderVOList.getList().get(orderVOList.getList().size() - 2).setReversePosition("dropup");
                }
            }

            // List<OrderJsonVO> orderJsonVOS = new ArrayList<>();
            //
            // for (OrderVO orderVOInner:orderVOList.getList()) {
            // OrderJsonVO orderJsonVO = new OrderJsonVO();
            // BeanUtils.copyProperties(orderJsonVO, orderVOInner);
            // orderJsonVOS.add(orderJsonVO);
            // }

            // 设置返回结果内容
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RECORDS_FILTERED, orderVOList.getTotal());
            returnMap.put(RECORDS_TOTAL, orderVOList.getTotal());
            returnMap.put(RETURN_DATA, orderVOList.getList());

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }

    /**
     * 取得tab页面头上的记录数
     * @param quickSearch
     * @param formVO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getTabCounts")
    public Map<String, Object> getTabCounts(String quickSearch, @RequestBody FormVO formVO) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            // 查询条件
            OrderVO param = new OrderVO();

            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null) {
                returnMap.put(JSON_RESULT, FAILED);
                returnMap.put(JSON_MSG, "当前用户失效");
                return returnMap;
            }

            String prjId = currentUser.getCurrentProject().getPrjId();

            param.setPrjId(prjId);
            // 快速查询
            quickSearch = URLDecoder.decode(quickSearch, "UTF-8");
            quickSearch = quickSearch.trim();
            if (StringUtils.isNotBlank(quickSearch)) {
                param.setSearchCondition(quickSearch);
            }

            if (formVO != null) {
                // 验视？
                if (StringUtils.isNotBlank(formVO.getHasInspected())) {
                    param.setHasInspected(formVO.getHasInspected());
                }
                // 平台交易号
                if (StringUtils.isNotBlank(formVO.getTradeCode())) {
                    param.setTradeCode(formVO.getTradeCode());
                }

                // 订单编号
                if (StringUtils.isNotBlank(formVO.getOrderCode())) {
                    param.setOrderCode(formVO.getOrderCode());
                }

                // 店铺
                if (StringUtils.isNotBlank(formVO.getShopId())) {
                    param.setShopId(formVO.getShopId());
                }

                // 来源
                if (StringUtils.isNotBlank(formVO.getShopType())) {
                    param.setShopType(formVO.getShopType());
                }

                // 拍单时间 开始日期
                if (StringUtils.isNotBlank(formVO.getOrderTimeS())) {
                    param.setOrderTimeS(formVO.getOrderTimeS());
                }

                // 付款时间 开始日期
                if (StringUtils.isNotBlank(formVO.getPaidDateS())) {
                    param.setPaidDateS(formVO.getPaidDateS());
                }

                // 发货时间 开始日期
                if (StringUtils.isNotBlank(formVO.getShipTimeS())) {
                    param.setShipTimeS(formVO.getShipTimeS());
                }

                // 完成时间 开始日期
                if (StringUtils.isNotBlank(formVO.getFinishTimeS())) {
                    param.setFinishTimeS(formVO.getFinishTimeS());
                }

                // 拍单时间 截止日期
                if (StringUtils.isNotBlank(formVO.getOrderTimeE())) {
                    param.setOrderTimeE(formVO.getOrderTimeE());
                }

                // 付款时间 截止日期
                if (StringUtils.isNotBlank(formVO.getPaidDateE())) {
                    param.setPaidDateE(formVO.getPaidDateE());
                }

                // 发货时间 截止日期
                if (StringUtils.isNotBlank(formVO.getShipTimeE())) {
                    param.setShipTimeE(formVO.getShipTimeE());
                }

                // 完成时间 截止日期
                if (StringUtils.isNotBlank(formVO.getFinishTimeE())) {
                    param.setFinishTimeE(formVO.getFinishTimeE());
                }

                // 规格商家编码
                if (StringUtils.isNotBlank(formVO.getSkuCode())) {
                    param.setSkuCode(formVO.getSkuCode());
                }

                // 顾客ID
                if (StringUtils.isNotBlank(formVO.getBuyerNick())) {
                    param.setBuyerNick(formVO.getBuyerNick());
                }

                // 平台商品编码
                if (StringUtils.isNotBlank(formVO.getEcItemCode())) {
                    param.setEcItemCode(formVO.getEcItemCode());
                }

                // 平台规格编码
                if (StringUtils.isNotBlank(formVO.getEcSkuCode())) {
                    param.setEcSkuCode(formVO.getEcSkuCode());
                }

                // 问题单类型
                if (StringUtils.isNotBlank(formVO.getErrorType())) {
                    if (formVO.getErrorType().contains("isOrderError")) {
                        param.setIsOrderError("y");
                    }

                    if (formVO.getErrorType().contains("isGoodsError")) {
                        param.setIsGoodsError("y");
                    }

                    if (formVO.getErrorType().contains("isLgstError")) {
                        param.setIsLgstError("y");
                    }

                    if (formVO.getErrorType().contains("isStockError")) {
                        param.setIsStockError("y");
                    }
                }

                // 标记
                if (StringUtils.isNotBlank(formVO.getStamp())) {
                    param.setStamp(formVO.getStamp());
                }

                // 售后
                if (StringUtils.isNotBlank(formVO.getCustomerService()) && "1".equals(formVO.getCustomerService())) {
                    param.setEvent("1");
                }
                if (StringUtils.isNotBlank(formVO.getCustomerService()) && "0".equals(formVO.getCustomerService())) {
                    param.setEvent("0");
                }
                // 合并
                if (StringUtils.isNotBlank(formVO.getCombStus())) {
                    param.setCombStus(formVO.getCombStus());
                }
            }

            List<OrderVO> list = orderService.findCounts(param);

            // 设置返回结果内容
            returnMap.put(JSON_RESULT, SUCCESS);
            int countWaitVerify = 0;
            int countWaitAssign = 0;
            int countShipping = 0;
            int countShipped = 0;
            int countComplete = 0;
            int countCancel = 0;

            if (list != null && list.size() > 0) {
                countWaitVerify = list.get(0).getCountWaitVerify() != null ? list.get(0).getCountWaitVerify().intValue() : 0;
                countWaitAssign = list.get(0).getCountWaitAssign() != null ? list.get(0).getCountWaitAssign().intValue() : 0;
                countShipping = list.get(0).getCountShipping() != null ? list.get(0).getCountShipping().intValue() : 0;
                countShipped = list.get(0).getCountShipped() != null ? list.get(0).getCountShipped().intValue() : 0;
                countComplete = list.get(0).getCountComplete() != null ? list.get(0).getCountComplete().intValue() : 0;
                countCancel = list.get(0).getCountCancel() != null ? list.get(0).getCountCancel().intValue() : 0;
            }
            returnMap.put("countWaitVerify", countWaitVerify);
            returnMap.put("countWaitAssign", countWaitAssign);
            returnMap.put("countShipping", countShipping);
            returnMap.put("countShipped", countShipped);
            returnMap.put("countComplete", countComplete);
            returnMap.put("countCancel", countCancel);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }

    // /**
    // * 取得仓库信息
    // *
    // * @return
    // * @throws Exception
    // */
    // @ResponseBody
    // @RequestMapping(value = "/getStorList", method = RequestMethod.GET)
    // public Map<String, Object> getStorList() throws Exception {
    // // 用于返回值的json对象
    // Map<String, Object> returnMap = new HashMap<String, Object>();
    // try {
    // // 用于返回值的json对象
    // Map<String, Object> resultMap = new HashMap<String, Object>();
    // CurrentUser currentUser =
    // WebContextFactory.getWebContext().getCurrentUser();
    // if (currentUser == null) {
    // resultMap.put(JSON_RESULT, FAILED);
    // resultMap.put(JSON_MSG, "当前用户失效");
    // return resultMap;
    // }
    //
    // List<StorVO> listStorVO =
    // orderService.findStor(currentUser.getCurrentProject().getPrjId());
    // // 设置返回结果内容
    // returnMap.put(JSON_RESULT, SUCCESS);
    // returnMap.put(RETURN_DATA, listStorVO);
    // } catch (Exception ex) {
    // logger.error(ex.getMessage(), ex);
    // returnMap.put(JSON_RESULT, FAILED);
    // returnMap.put(JSON_MSG, ex.getMessage());
    // }
    // // 生成返回结果json串，null内容也需要返回
    // return returnMap;
    // }
    //
    // /**
    // * 取得物流公司
    // *
    // * @return
    // * @throws Exception
    // */
    // @ResponseBody
    // @RequestMapping(value = "/getLgstList", method = RequestMethod.GET)
    // public Map<String, Object> getLgstList() throws Exception {
    // // 用于返回值的json对象
    // Map<String, Object> returnMap = new HashMap<String, Object>();
    // try {
    // // 用于返回值的json对象
    // Map<String, Object> resultMap = new HashMap<String, Object>();
    // CurrentUser currentUser =
    // WebContextFactory.getWebContext().getCurrentUser();
    // if (currentUser == null) {
    // resultMap.put(JSON_RESULT, FAILED);
    // resultMap.put(JSON_MSG, "当前用户失效");
    // return resultMap;
    // }
    //
    // List<LgstVO> listLgstVO =
    // orderService.findLgst(currentUser.getCurrentProject().getPrjId());
    // // 设置返回结果内容
    // returnMap.put(JSON_RESULT, SUCCESS);
    // returnMap.put(RETURN_DATA, listLgstVO);
    // } catch (Exception ex) {
    // logger.error(ex.getMessage(), ex);
    // returnMap.put(JSON_RESULT, FAILED);
    // returnMap.put(JSON_MSG, ex.getMessage());
    // }
    // // 生成返回结果json串，null内容也需要返回
    // return returnMap;
    // }

    /**
     * 按照projectId取得goods，goodsSku
     *
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getGoodsSku", method = RequestMethod.GET)
    public Map<String, Object> getGoodsSku() throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null) {
                returnMap.put(JSON_RESULT, FAILED);
                returnMap.put(JSON_MSG, "当前用户失效");
                return returnMap;
            }

            String prjId = currentUser.getCurrentProject().getPrjId();

            PrjSkuVO prjSkuVO = new PrjSkuVO();
            prjSkuVO.setPrjId(prjId);

            List<PrjSkuVO> listPrjSkuVO = prjSkuService.getGoodsSku(prjSkuVO);
            // 设置返回结果内容
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, listPrjSkuVO);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }

    /**
     * 分派订单
     *
     * @param assigObject 逗号分割的字符串
     */
    @ResponseBody
    @RequestMapping(value = "/assignOrder")
    @SystemControllerLog(module = "系统订单", operation = "分派订单", keyCode = "orderId")
    public Map<String, Object> assignOrder(@RequestBody AssignVO assigObject) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null) {
                returnMap.put(JSON_RESULT, FAILED);
                returnMap.put(JSON_MSG, "当前用户失效");
                return returnMap;
            }

            Map<String, Object> serviceMap = orderService.assignOrder(assigObject, currentUser.getCurrentProject().getPrjShortName());
            returnMap.put(JSON_RESULT, serviceMap.get("success"));
            returnMap.put(JSON_MSG, serviceMap.get("errormessage"));
            returnMap.put(RETURN_DATA, serviceMap.get(RETURN_DATA));
            returnMap.put("questionhasrefund", serviceMap.get("questionhasrefund"));
            returnMap.put("questionLgstId", serviceMap.get("questionLgstId"));
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", assigObject.getOrderId());
        return returnMap;
    }

    /**
     * 订单合并：
     * <p>
     * 合并的条件：当一条以上的订单为同网店、同顾客ID、同收货人、同收货地址，系统判断这些订单如果网店的订单合并开关开启，订单状态为待审核未废弃，未合并过，未被锁定、
     * 自动状态、不存在售后单据、不存在未废弃的发票、未在待审核队列中，那么系统进行自动合并，合并时仅合并新下载到系统的订单与系统中已存在且未合并的多条订单。
     * 合并的处理如下：合并来源单号、买家留言、卖家备注和各种金额。
     * <p>
     * 合并后处理：更新子订单合并状态及合并后的GUID；删除合并前所有子订单中赠品；生成合并订单以及订单商品明细（主订单信息为拍单时间最早的订单为准，例拍单时间、付款
     * 时间等）
     *
     * @param orderIdList 系统订单id列表
     */
    @ResponseBody
    // @RequestMapping(value = "/")
    public Map<String, Object> mergeOrder(List<String> orderIdList) {
        return null;
    }

    // /**
    // * 修改系统订单
    // *
    // * @param order 系统订单VO
    // */
    // @ResponseBody
    //// @RequestMapping(value = "/")
    // private Map<String, Object> updateOrder(OrderVO order) {
    // return null;
    // }

    /**
     * 创建系统订单
     *
     * @param orderInfo 系统订单VO
     */
    @ResponseBody
    @RequestMapping(value = "/createOrderImport")
    @SystemControllerLog(module = "系统订单", operation = "创建系统订单", keyCode = "orderId")
    public Map<String, Object> createOrderImport(@RequestBody OrderInfoList orderInfoList) throws Exception {

        // 用于返回值的json对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户失效");
            return resultMap;
        }
        Map<String, String> mapParm = new HashMap<String, String>();
        Map<String, String> enterpriseMap = currentUser.getEnterpriseMap();
        if (enterpriseMap == null || enterpriseMap.size() == 0) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户没有企业ID");
            return resultMap;
        }
        String entId = "";
        String entName = "";
        for (Map.Entry<String, String> entry : enterpriseMap.entrySet()) {
            entId = entry.getKey();
            entName = entry.getValue();
        }
        mapParm.put("entId", entId);
        mapParm.put("entName", entName);
        mapParm.put("prjId", currentUser.getCurrentProject().getPrjId());
        mapParm.put("prjName", currentUser.getCurrentProject().getPrjName());
        mapParm.put("prjShortName", currentUser.getCurrentProject().getPrjShortName());

        try{
            Map<String, Object> map = orderService.modifyOrderData(orderInfoList, mapParm);
            resultMap.put(RESULT_SUCCESS_FLG, map.get("isOk"));
            resultMap.put(RESULT_ROW_COUNT, map.get("count"));
            resultMap.put(RESULT_ERROR_MESSAGE, map.get("errormessage"));
        } catch (Exception ex){
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, ex.getMessage());
            return resultMap;
        }
        return resultMap;
    }

    /**
     * 创建系统订单
     *
     * @param orderInfo 系统订单VO
     */
    @ResponseBody
    @RequestMapping(value = "/createOrder")
    @SystemControllerLog(module = "系统订单", operation = "创建系统订单", keyCode = "orderId")
    public Map<String, Object> createOrder(@RequestBody OrderInfoVO orderInfo) throws Exception {

        // 用于返回值的json对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户失效");
            return resultMap;
        }
        Map<String, String> mapParm = new HashMap<String, String>();
        Map<String, String> enterpriseMap = currentUser.getEnterpriseMap();
        if (enterpriseMap == null || enterpriseMap.size() == 0) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户没有企业ID");
            return resultMap;
        }
        String entId = "";
        String entName = "";
        for (Map.Entry<String, String> entry : enterpriseMap.entrySet()) {
            entId = entry.getKey();
            entName = entry.getValue();
        }
        mapParm.put("entId", entId);
        mapParm.put("entName", entName);
        mapParm.put("prjId", currentUser.getCurrentProject().getPrjId());
        mapParm.put("prjName", currentUser.getCurrentProject().getPrjName());
        mapParm.put("prjShortName", currentUser.getCurrentProject().getPrjShortName());
        if (orderInfo != null) {
            orderInfo.setType("all");
        }
        Map<String, Object> map = orderService.modifyOrderData(orderInfo, mapParm);
        resultMap.put(RESULT_SUCCESS_FLG, map.get("isOk"));
        resultMap.put(RESULT_ROW_COUNT, map.get("count"));
        resultMap.put(RESULT_ERROR_MESSAGE, map.get("errormessage"));
        resultMap.put("orderId", orderInfo.getOrder().getOrderId());
        return resultMap;
    }

    /**
     * 检查订单问题 1.订单问题：如果订单存在以下情况会被拦截为订单问题单
     * 所属网店开启了自动合并订单，系统中存在可与之合并（同网店、同顾客ID、同收货人、同收货地址）的已审核状态但不可操作的订单；
     * 有买家留言（网店开启了“有买家留言”拦截为订单问题）； 有卖家备注（网店开启了“有卖家备注”拦截为订单问题）；
     * 收货地址超长（网店开启了“收货地址超过N个字节拦截为订单问题）； 存在未废弃退款单； 系统状态和平台状态不一致（仅淘宝）
     * 合并订单的原订单数量大于15； 收货人、手机、电话信息有为空的； 身份证号为空（网店开启了“验证身份证号码“）；
     *
     * @param order 系统订单VO
     */
    @ResponseBody
    // @RequestMapping(value = "/")
    private Map<String, Object> validateOrderProblems(OrderVO order) {
        return null;
    }

    /**
     * 自动审核分派 根据设置进行审核分派：按网店设置订单下载多长时间以后才能进行自动审核这个参数
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     */
    @ResponseBody
    // @RequestMapping(value = "/")
    public Map<String, Object> autoVerifyAndAssignOrder(Date startTime, Date endTime) {
        return null;
    }

    /**
     * 订单自动分派的逻辑： 自动审核成功的订单如果商品明细全部为虚拟商品，订单状态变为待分派，且打成手动状态；
     * 如果商品明细中存在非虚拟商品，系统自动做以下操作： 1.自动创建发票（订单需要开发票，且不存在未废弃发票）；
     * 2.生成出库单：以订单发货仓库、收货人、收货地址、非虚拟商品明细、订购数量等信息生待发货状态的订单出库单。
     * <p>
     * 订单自动审核分派成功，状态变为发货中。
     *
     * @param orderId 系统订单id
     */
    @ResponseBody
    // @RequestMapping(value = "/")
    public Map<String, Object> autoAssignOrder(String orderId) {
        return null;
    }

    /**
     * 生成系统订单
     *
     * @param ecTradeId 平台订单id
     */
    @ResponseBody
    // @RequestMapping(value = "/")
    public Map<String, Object> manualGenerateOrder(String ecTradeId) {
        return null;
    }

    /**
     * 废弃系统订单
     * 选中一条或多条订单，操作废弃，如果可以废弃要提示“废弃订单系统将无法继续处理，将可能导致漏发，确认废弃吗？”，并且要输入废弃原因（文本）。废弃成功，订单变为已废
     * 弃状态。如果订单已不存在（被取消合并）、已归档、已废弃、自动处理、状态不是待审核的，不能被废弃。
     *
     * @param orderId
     * @param cancelRemark
     * @param strType      y:废弃，n:取消废弃
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/abandonOrder")
    @SystemControllerLog(module = "系统订单", operation = "废弃系统订单", keyCode = "orderId")
    public Map<String, Object> abandonOrder(String orderId, String cancelRemark, String strType) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            Map<String, Object> serviceMap = orderService.abandonOrder(orderId, cancelRemark, strType);
            returnMap.put(JSON_RESULT, serviceMap.get(JSON_RESULT));
            returnMap.put(RETURN_DATA, serviceMap.get(RETURN_DATA));
            returnMap.put(JSON_MSG, serviceMap.get(JSON_MSG));
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 设置自动
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/doOnAuto")
    @SystemControllerLog(module = "系统订单", operation = "设置自动", keyCode = "orderId")
    public Map<String, Object> doOnAuto(String orderId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            Map<String, Object> serviceMap = orderService.setOrderAutomatically(orderId);
            returnMap.put(JSON_RESULT, serviceMap.get(JSON_RESULT));
            returnMap.put(RETURN_DATA, serviceMap.get(RETURN_DATA));
            returnMap.put(JSON_MSG, serviceMap.get(JSON_MSG));
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 设置手动
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/doOffAuto")
    @SystemControllerLog(module = "系统订单", operation = "设置手动", keyCode = "orderId")
    public Map<String, Object> doOffAuto(String orderId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            Map<String, Object> serviceMap = orderService.setOrderManually(orderId);
            returnMap.put(JSON_RESULT, serviceMap.get(JSON_RESULT));
            returnMap.put(RETURN_DATA, serviceMap.get(RETURN_DATA));
            returnMap.put(JSON_MSG, serviceMap.get(JSON_MSG));
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 锁定
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/doOnLock")
    @SystemControllerLog(module = "系统订单", operation = "锁定订单", keyCode = "orderId")
    public Map<String, Object> doOnLock(String orderId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            Map<String, Object> serviceMap = orderService.lockOrders(orderId);
            returnMap.put(JSON_RESULT, serviceMap.get(JSON_RESULT));
            returnMap.put(RETURN_DATA, serviceMap.get(RETURN_DATA));
            returnMap.put(JSON_MSG, serviceMap.get(JSON_MSG));
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 解除锁定
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/doCheckOffLock")
    public Map<String, Object> doCheckOffLock(String orderId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            Map<String, String> serviceMap = orderService.checkOffLockOrders(orderId);
            returnMap.put(JSON_RESULT, serviceMap.get(JSON_RESULT));
            returnMap.put(RETURN_DATA, serviceMap.get(RETURN_DATA));
            returnMap.put(JSON_MSG, serviceMap.get(JSON_MSG));
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, "");
            returnMap.put(JSON_MSG, ex.getMessage());
        }

        return returnMap;
    }

    /**
     * 解除锁定
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/doOffLock")
    @SystemControllerLog(module = "系统订单", operation = "解除锁定", keyCode = "orderId")
    public Map<String, Object> doOffLock(String orderId, String question) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            if ("1".equals(question)) {
                Map<String, String> serviceMap = orderService.checkOffLockOrders(orderId);
                // 当校验以后订单不存在未废弃或者未完成的退款单,直接解锁
                if (StringUtils.isBlank(serviceMap.get(RETURN_DATA))) {
                    Map<String, Object> serviceMaps = orderService.unlockOrders(orderId);
                    returnMap.put(JSON_RESULT, serviceMaps.get(JSON_RESULT));
                    returnMap.put(RETURN_DATA, serviceMaps.get(RETURN_DATA));
                    returnMap.put(JSON_MSG, serviceMaps.get(JSON_MSG));
                } else {
                    returnMap.put(JSON_RESULT, SUCCESS);
                    returnMap.put(RETURN_DATA, serviceMap.get(RETURN_DATA));
                    returnMap.put(JSON_MSG, serviceMap.get(JSON_MSG));
                    returnMap.put("question", serviceMap.get(JSON_MSG));
                    returnMap.put("questionId", orderId);
                }
            } else {
                Map<String, Object> serviceMap = orderService.unlockOrders(orderId);
                returnMap.put(JSON_RESULT, serviceMap.get(JSON_RESULT));
                returnMap.put(RETURN_DATA, serviceMap.get(RETURN_DATA));
                returnMap.put(JSON_MSG, serviceMap.get(JSON_MSG));
            }
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 验视
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/inspectedOrder")
    @SystemControllerLog(module = "系统订单", operation = "验视", keyCode = "orderId")
    public Map<String, Object> inspectedOrder(String orderId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            int count = orderService.inspectedOrder(orderId, "y");
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, count);
            if (count == -1) {
                returnMap.put(JSON_MSG, "自动状态的订单无法验视");
            }
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 取消验视
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/cancelInspectedOrder")
    @SystemControllerLog(module = "系统订单", operation = "取消验视", keyCode = "orderId")
    public Map<String, Object> cancelInspectedOrder(String orderId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            int count = orderService.inspectedOrder(orderId, "n");
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, count);
            if (count == -1) {
                returnMap.put(JSON_MSG, "自动状态的订单无法取消验视");
            }
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 查询系统订单
     *
     * @param param   系统订单VO
     * @param isPaged 是否分页
     */
    @ResponseBody
    // @RequestMapping(value = "/")
    public Map<String, Object> getOrders(OrderVO param, boolean isPaged) {
        return null;
    }

    /**
     * 取消合并： 可以操作取消合并的订单要满足的条件有：已合并、未锁定的待审核且未废弃状态的订单；未建立售后事件、售后单据及有效发票；订单未全额退款。
     * 只能选择一条符合条件的订单操作取消合并，通过校验条件系统会将订单拆分恢复为合并前的多条订单，未通过校验的要有具体提示。所有订单记录日志。
     *
     * @param orderId 系统订单id
     */
    @ResponseBody
    @RequestMapping(value = "/cancelMerger")
    @SystemControllerLog(module = "系统订单", operation = "取消合并", keyCode = "orderId")
    public Map<String, Object> cancelMerger(String orderId) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            Map<String, Object> serviceMap = orderService.cancelMerger(orderId);
            returnMap.put(JSON_RESULT, serviceMap.get(JSON_RESULT));
            returnMap.put(JSON_MSG, serviceMap.get(JSON_MSG));
            returnMap.put(RETURN_DATA, serviceMap.get(RETURN_DATA));
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 手动审核分派
     *
     * @param orderId 系统订单id
     */
    @ResponseBody
    // @RequestMapping(value = "/")
    public Map<String, Object> manualVerifyAndAssignOrder(String orderId) {
        return null;
    }

    /**
     * 审核订单
     *
     * @param orderId 系统订单id
     */
    @ResponseBody
    @RequestMapping(value = "/auditOrder")
    @SystemControllerLog(module = "系统订单", operation = "审核订单", keyCode = "orderId")
    public Map<String, Object> auditOrder(String orderId, String msg, String qAuditOrder) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            Map<String, Object> serviceMap = orderService.verifyOrder(orderId, qAuditOrder);
            returnMap.put(JSON_RESULT, serviceMap.get(JSON_RESULT));

            returnMap.put(JSON_MSG, msg + serviceMap.get(JSON_MSG));
            returnMap.put("confirmMsg", serviceMap.get("confirmMsg"));
            returnMap.put(RETURN_DATA, serviceMap.get(RETURN_DATA));
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
            returnMap.put("confirmMsg", "");
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 取消审核订单
     *
     * @param orderId 系统订单id
     */
    @ResponseBody
    @RequestMapping(value = "/unAuditOrder")
    @SystemControllerLog(module = "系统订单", operation = "取消审核订单", keyCode = "orderId")
    public Map<String, Object> unAuditOrder(String orderId) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            Map<String, Object> serviceMap = orderService.unAuditOrder(orderId);
            returnMap.put(JSON_RESULT, serviceMap.get(JSON_RESULT));

            returnMap.put(JSON_MSG, serviceMap.get(JSON_MSG));
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 完成订单，选择一条或多条订单，操作完成。只有已发货商品且未被锁定、未废弃的已审核的订单可以被完成，如果校验未通过要提示出完成**条订单，以下订单无法完成，提示出
     * 原因列表。 订单完成状态变为已完成。
     *
     * @param orderId 系统订单id
     */
    @ResponseBody
    @RequestMapping(value = "/finishOrder")
    @SystemControllerLog(module = "系统订单", operation = "完成订单", keyCode = "orderId")
    public Map<String, Object> finishOrder(String orderId) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            Map<String, Object> serviceMap = orderService.finishOrder(orderId);
            returnMap.put(JSON_RESULT, serviceMap.get(JSON_RESULT));
            returnMap.put(JSON_MSG, serviceMap.get(JSON_MSG));
            returnMap.put(RETURN_DATA, serviceMap.get(RETURN_DATA));
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    // /**
    // * 锁定订单
    // *
    // * @param orderId 系统订单id
    // */
    // @ResponseBody
    //// @RequestMapping(value = "/")
    // public Map<String, Object> lockOrders(String orderId) {
    // return null;
    // }
    //
    // /**
    // * 解锁订单
    // *
    // * @param orderId 系统订单id
    // */
    // @ResponseBody
    //// @RequestMapping(value = "/")
    // public Map<String, Object> unlockOrders(String orderId) {
    // return null;
    // }

    // /**
    // * 设置为自动订单
    // *
    // * @param orderId 系统订单id
    // */
    // @ResponseBody
    //// @RequestMapping(value = "/")
    // public Map<String, Object> setOrderAutomatically(String orderId) {
    // return null;
    // }

    // /**
    // * 设置为手动订单
    // *
    // * @param orderId 系统订单id
    // */
    // @ResponseBody
    //// @RequestMapping(value = "/")
    // public Map<String, Object> setOrderManually(String orderId) {
    // return null;
    // }

    // /**
    // * 恢复已删除订单商品
    // * 修改待审核状态订单时，点击“恢复已删除商品”时，展示出之前已删除的该订单中商品明细（内容同订单明细），可以批量选择恢复至订单商品明细。
    // *
    // * @param orderId 系统订单id
    // * @param OrderGoodsIdList
    // */
    // @ResponseBody
    //// @RequestMapping(value = "/")
    // public Map<String, Object> withdrawDeletedOrders(String orderId,
    // List<String> OrderGoodsIdList) {
    // return null;
    // }

    /**
     * 申请取消出库 在订单发货信息tab中选择一条出库单，点击申请取消，判断出库单状态对应以下处理： 1.
     * 未进行任何操作（未录入快递单号、未打印快递单、未打印发货单），系统直接取消出库单（状态变为已取消），关联订单如果无其他未取消出库单，订单状态回滚至待审核；
     * 2.
     * 出库单已操作或已系统发货或已平台发货，出库单标识申请取消，由库房操作是否同意取消；如果仓库反馈同意取消，系统取消该出库单（状态变为已取消），关联订单如果无其他未
     * 取消出库单，订单状态回滚至待审核； 3.订单出库单已发货，不能申请取消；
     *
     * @param orderId    系统订单id
     * @param outBoundId
     */
    @ResponseBody
    // @RequestMapping(value = "/")
    public Map<String, Object> askForStopOutBound(String orderId, String outBoundId) {
        return null;
    }

    /**
     * 同意取消出库单，由仓库调用本接口 如果仓库反馈同意取消，系统取消该出库单（状态变为已取消），关联订单如果无其他未取消出库单，订单状态回滚至待审核；
     *
     * @param outBoundId
     */
    @ResponseBody
    // @RequestMapping(value = "/")
    public Map<String, Object> agreeStopOutBound(String outBoundId) {
        return null;
    }

    /**
     * 批量修改物流公司
     * @param orderId
     * @param lgstId
     * @param lgstName
     * @param lgstCode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchUpdateLogisticsCompany")
    @SystemControllerLog(module = "系统订单", operation = "批量修改物流公司", keyCode = "orderId")
    public Map<String, Object> batchUpdateLogisticsCompany(String orderId, String lgstId, String lgstName, String lgstCode) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            int count = orderService.batchUpdateLogisticsCompany(orderId, lgstId, lgstName, lgstCode);
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, count);
            if (count == -1) {
                returnMap.put(JSON_MSG, "自动状态的订单不能设置物流公司");
            }
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 批量修改发货仓库
     * @param orderId
     * @param storId
     * @param storName
     * @param storCode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchUpdateShipmentWarehouse")
    @SystemControllerLog(module = "系统订单", operation = "批量修改发货仓库", keyCode = "orderId")
    public Map<String, Object> batchUpdateShipmentWarehouse(String orderId, String storId, String storName, String storCode) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            int count = orderService.batchUpdateShipmentWarehouse(orderId, storId, storName, storCode);
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, count);
            if (count == -1) {
                returnMap.put(JSON_MSG, "自动状态的订单不能修改发货仓库");
            }
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 批量修改内部备注
     * @param orderId
     * @param addRemark
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchUpdateOrderMemo")
    @SystemControllerLog(module = "系统订单", operation = "批量修改内部备注", keyCode = "orderId")
    public Map<String, Object> batchUpdateOrderMemo(String orderId, String addRemark) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            String result = orderService.batchUpdateOrderMemo(orderId, addRemark);
            returnMap.put(JSON_RESULT, SUCCESS);
            // returnMap.put(RETURN_DATA, count);
            returnMap.put(JSON_MSG, result);
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderId);
        return returnMap;
    }

    /**
     * 检测订单商品问题
     *
     * @param orderGoodsList
     */
    private Map<String, Object> validateOrderGoodsProblems(List<OrderSku> orderGoodsList) {
        return null;
    }

    /**
     * @param orderIdList 要合并的订单id列表
     */
    @ResponseBody
    @RequestMapping(value = "/forceMergeOrdersManual")
    public Map<String, Object> forceMergeOrdersManual(List<String> orderIdList) {
        return null;
    }

    // orderSku相关的操作

    /**
     * 取得orderInfo
     *
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getOrderInfo", method = RequestMethod.GET)
    public Map<String, Object> getOrderInfo(String orderId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            // 用于返回值的json对象
            Map<String, Object> resultMap = new HashMap<String, Object>();

            // orderId如果是空就返回错误信息
            if (StringUtils.isBlank(orderId)) {
                resultMap.put(JSON_RESULT, FAILED);
                resultMap.put(JSON_MSG, "orderId是空");
                return resultMap;
            }

            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null) {
                resultMap.put(JSON_RESULT, FAILED);
                resultMap.put(JSON_MSG, "当前用户失效");
                return resultMap;
            }

            OrderVO param = new OrderVO();
            param.setOrderId(orderId);

            // 获取分页数据
            PageInfo<OrderVO> orderVOList = orderService.findByWhere(0, Integer.MAX_VALUE, param);
            if (orderVOList != null && orderVOList.getList().size() > 0) {
                returnMap.put(JSON_RESULT, SUCCESS);
                returnMap.put(RETURN_DATA, orderVOList.getList().get(0));
            } else {
                returnMap.put(JSON_RESULT, FAILED);
                resultMap.put(JSON_MSG, "orderId是" + orderId + "的数据不存在");
                returnMap.put(RETURN_DATA, null);
            }
            // 设置返回结果内容

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }

    /**
     * getOrderForEvent:查询订单，用于创建售后事件时
     *
     * @param buyerNick
     * @param tradeCode
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getOrderForEvent", method = RequestMethod.GET)
    public Map<String, Object> getOrderForEvent(String buyerNick, String tradeCode,
                                                @RequestParam(required = false, defaultValue = "0") int start, @RequestParam(required = false, defaultValue = "10") int length)
            throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            // 用于返回值的json对象
            Map<String, Object> resultMap = new HashMap<String, Object>();

            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null) {
                resultMap.put(JSON_RESULT, FAILED);
                resultMap.put(JSON_MSG, "当前用户失效");
                return resultMap;
            }
            // 获取当前登录人所属项目
            String projectId = currentUser.getCurrentProject().getPrjId();

            OrderVOForEvent param = new OrderVOForEvent();
            param.setPrjId(projectId);

            // 平台交易号
            if (StringUtils.isNotBlank(tradeCode)) {
                tradeCode = URLDecoder.decode(tradeCode, "UTF-8");
                param.setTradeCode(tradeCode);
            }

            // 顾客ID
            if (StringUtils.isNotBlank(buyerNick)) {
                buyerNick = URLDecoder.decode(buyerNick, "UTF-8");
                param.setBuyerNick(buyerNick);
            }

            // 处理分页信息
            if (length == -1) {
                length = Integer.MAX_VALUE;
            }
            int page = start / length + 1;

            // 获取分页数据
            PageInfo<OrderVOForEvent> orderVOList = orderService.findByWhere(page, length, param);
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RECORDS_FILTERED, orderVOList.getTotal());
            returnMap.put(RECORDS_TOTAL, orderVOList.getTotal());
            returnMap.put(RETURN_DATA, orderVOList.getList());

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }

    /**
     * 取得orderInfo根据code取得
     *
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getOrderInfoByCode", method = RequestMethod.GET)
    public Map<String, Object> getOrderInfoByCode(String orderCode) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            // 用于返回值的json对象
            Map<String, Object> resultMap = new HashMap<String, Object>();

            // orderId如果是空就返回错误信息
            if (StringUtils.isBlank(orderCode)) {
                resultMap.put(JSON_RESULT, FAILED);
                resultMap.put(JSON_MSG, "orderCode是空");
                return resultMap;
            }

            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null) {
                resultMap.put(JSON_RESULT, FAILED);
                resultMap.put(JSON_MSG, "当前用户失效");
                return resultMap;
            }

            OrderVO param = new OrderVO();
            param.setOrderCode(orderCode);

            // 获取分页数据
            PageInfo<OrderVO> orderVOList = orderService.findByWhere(0, Integer.MAX_VALUE, param);
            if (orderVOList != null && orderVOList.getList().size() > 0) {
                returnMap.put(JSON_RESULT, SUCCESS);
                returnMap.put(RETURN_DATA, orderVOList.getList().get(0));
            } else {
                returnMap.put(JSON_RESULT, FAILED);
                resultMap.put(JSON_MSG, "orderCode是" + orderCode + "的数据不存在");
                returnMap.put(JSON_MSG, "您输入的来源单号有误，请正确输入");
                returnMap.put(RETURN_DATA, null);
            }
            // 设置返回结果内容

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/updateOrder")
    @SystemControllerLog(module = "系统订单", operation = "修改订单", keyCode = "orderId")
    public Map<String, Object> updateOrder(@RequestBody Order order) throws Exception {

        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            returnMap.put(RESULT_SUCCESS_FLG, "n");
            returnMap.put(RESULT_ERROR_MESSAGE, "当前用户失效");
            return returnMap;
        }
        Map<String, String> mapParm = new HashMap<String, String>();
        Map<String, String> enterpriseMap = currentUser.getEnterpriseMap();
        if (enterpriseMap == null || enterpriseMap.size() == 0) {
            returnMap.put(RESULT_SUCCESS_FLG, "n");
            returnMap.put(RESULT_ERROR_MESSAGE, "当前用户没有企业ID");
            return returnMap;
        }
        String entId = "";
        String entName = "";
        for (Map.Entry<String, String> entry : enterpriseMap.entrySet()) {
            entId = entry.getKey();
            entName = entry.getValue();
        }
        mapParm.put("entId", entId);
        mapParm.put("entName", entName);
        mapParm.put("prjId", currentUser.getCurrentProject().getPrjId());
        mapParm.put("prjName", currentUser.getCurrentProject().getPrjName());
        mapParm.put("prjShortName", currentUser.getCurrentProject().getPrjShortName());

        OrderInfoVO orderInfo = new OrderInfoVO();
        orderInfo.setOrder(order);
        orderInfo.setAction("update");
        orderInfo.setType("order");
        Map<String, Object> map = orderService.modifyOrderData(orderInfo, mapParm);
        returnMap.put(RESULT_SUCCESS_FLG, map.get("isOk"));
        returnMap.put(RESULT_ROW_COUNT, map.get("count"));
        returnMap.put(RESULT_ERROR_MESSAGE, map.get("errormessage"));
        returnMap.put("orderId", order.getOrderId());
        return returnMap;

    }

    /**
     * 手动订单下载
     *
     * @param downLoadVO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/downLoadOrder")
    public Map<String, Object> downLoadOrder(@RequestBody DownLoadVO downLoadVO) {

        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            returnMap.put(RESULT_SUCCESS_FLG, "n");
            returnMap.put(RESULT_ERROR_MESSAGE, "当前用户失效");
            return returnMap;
        }

        try {
            Map<String, String> map = orderService.downLoadOrder(downLoadVO);

            returnMap.put(RESULT_SUCCESS_FLG, map.get("success"));
            returnMap.put(RETURN_DATA, map.get("data"));
            returnMap.put(RESULT_ERROR_MESSAGE, map.get("errormessage"));
        } catch (Exception e) {
            returnMap.put(RESULT_SUCCESS_FLG, "n");
            returnMap.put(RETURN_DATA, "");
            returnMap.put(RESULT_ERROR_MESSAGE, e.getMessage());
        }

        return returnMap;

    }

    /**
     * orderSkuLIst
     *
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getTradeCodeNew")
    public Map<String, Object> getTradeCodeNew() throws Exception {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("data", CodeUtil.getCode());
        returnMap.put(JSON_RESULT, SUCCESS);

        return returnMap;
    }

    /**
     * orderSkuLIst
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getOrderSku")
    public Map<String, Object> getOrderSku(String orderId, String haveVirt) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();

        // 取得规格
        OrderSkuVO orderSkuVO = new OrderSkuVO();
        orderSkuVO.setOrderId(orderId);
        // haveVirt=y则虚拟商品也显示，haveVirt=n则只显示非虚拟物品
        if (StringUtils.isNotBlank(haveVirt) && haveVirt.equalsIgnoreCase("n")) {
            orderSkuVO.setIsVirt(YesNoConst.NO);
        }
        List<OrderSkuVO> arrSku = StringUtils.isBlank(orderId) ? null : orderService.getOrderSku(orderSkuVO);

        returnMap.put("data", arrSku);
        returnMap.put(JSON_RESULT, SUCCESS);

        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/updateOrderSkus")
    @SystemControllerLog(module = "系统订单", operation = "修改订单商品")
    public Map<String, Object> updateOrderSkus(@RequestBody List<OrderSkuVO> orderSkuVOList, String updateSkuQ) throws Exception {

        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            returnMap.put(RESULT_SUCCESS_FLG, "n");
            returnMap.put(RESULT_ERROR_MESSAGE, "当前用户失效");
            return returnMap;
        }
        Map<String, String> mapParm = new HashMap<String, String>();
        Map<String, String> enterpriseMap = currentUser.getEnterpriseMap();
        if (enterpriseMap == null || enterpriseMap.size() == 0) {
            returnMap.put(RESULT_SUCCESS_FLG, "n");
            returnMap.put(RESULT_ERROR_MESSAGE, "当前用户没有企业ID");
            return returnMap;
        }
        String entId = "";
        String entName = "";
        for (Map.Entry<String, String> entry : enterpriseMap.entrySet()) {
            entId = entry.getKey();
            entName = entry.getValue();
        }
        mapParm.put("entId", entId);
        mapParm.put("entName", entName);
        mapParm.put("prjId", currentUser.getCurrentProject().getPrjId());
        mapParm.put("prjName", currentUser.getCurrentProject().getPrjName());
        mapParm.put("prjShortName", currentUser.getCurrentProject().getPrjShortName());
        mapParm.put("updateSkuQ", updateSkuQ);
        Order order = new Order();
        OrderInfoVO orderInfo = new OrderInfoVO();
        orderInfo.setOrder(order);
        orderInfo.setAction("update");
        orderInfo.setArrSku(orderSkuVOList);
        orderInfo.setType("sku");
        Map<String, Object> map = orderService.modifyOrderData(orderInfo, mapParm);
        returnMap.put(RESULT_SUCCESS_FLG, map.get("isOk"));
        returnMap.put(RESULT_ROW_COUNT, map.get("count"));
        returnMap.put(RESULT_ERROR_MESSAGE, map.get("errormessage"));
        returnMap.put(RETURN_DATA, map.get("qlist"));
        returnMap.put("qmsg", map.get("qmsg"));
        return returnMap;

    }
    
    @ResponseBody
    @RequestMapping(value = "/updateOrderSkuInfo")
    @SystemControllerLog(module = "系统订单", operation = "替换订单商品")
    public Map<String, Object> updateOrderSkuInfo(@RequestBody OrderSkuVO orderSkuVO) throws Exception {
        JSONObject returnMap = new JSONObject();
        try {
            orderService.updateOrderSku4Replace(orderSkuVO);
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(JSON_MSG, "替换完成");
        }catch(Exception e) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, "替换失败，请联系管理员");
        }
        
        return returnMap;
        
    }

    /**
     * 获取系统订单售后信息 wangxing add
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getOrderAftersaleInfo")
    public Map<String, Object> getOrderAftersaleInfo(String orderId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            List<OrderVO> AftersaleList = StringUtils.isBlank(orderId) ? null : orderService.getOrderAftersaleInfo(orderId);
            // returnMap.put("OrderAftersale", AftersaleList);
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, AftersaleList);
        } catch (Exception ex) {
            // logUtil.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        return returnMap;
    }

    /**
     * 取消发货单(待发货)，修改订单信息
     *
     * @param shiporderVO     发货单Id
     * @return 结果Map
     */
    @ResponseBody
    @RequestMapping(value = {"/cancelShiporder"})
    @SystemControllerLog(module = "系统订单", operation = "取消发货单(待发货)，修改订单信息", keyCode = "shiporderId")
    public Map<String, Object> cancelShiporder(@RequestBody ShiporderVO shiporderVO) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            Map<String, Object> map = orderService.cancelShiporder(shiporderVO);
            returnMap.put(JSON_RESULT, map.get(JSON_RESULT));
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("shiporderId", shiporderVO.getShiporderId());
        return returnMap;
    }

    /**
     * 批量修改平台备注
     *
     * @param formJson
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchUpdateOrderSalesRemark")
    @SystemControllerLog(module = "系统订单", operation = "批量修改平台备注", keyCode = "orderId")
    public Map<String, Object> batchUpdateOrderSalesRemark(String formJson) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();

        String formJsonDecode = URLDecoder.decode(formJson, "UTF-8");
        OrderVO orderVO = JSONObject.parseObject(formJsonDecode, OrderVO.class);

        if (StringUtils.isBlank(orderVO.getSalesRemark())) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, "请填写卖家备注");
            return returnMap;
        }

        try {
            String result = orderService.batchUpdateOrderSalesRemark(orderVO.getOrderId(), orderVO.getSalesRemark(), orderVO.getTbFlagCode());
            returnMap.put(JSON_RESULT, SUCCESS);
            // returnMap.put(RETURN_DATA, count);
            returnMap.put(JSON_MSG, result);
        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RETURN_DATA, 0);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("orderId", orderVO.getOrderId());
        return returnMap;
    }

    /**
     * 验证需要导出的条件
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/exportCheckOrder")
    public Map<String, Object> exportCheckStock() throws Exception {
        logger.info("系统订单导出check：/order/exportCheckOrder");
        Map<String, Object> returnJSON = new HashMap<String, Object>();
        try {

            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null){
                returnJSON.put(JSON_RESULT, FAILED);
                returnJSON.put(JSON_MSG, "查询当前登录人失败");
                return returnJSON;
            }

            // 获取当前登录人所属项目
            String projectId = currentUser.getCurrentProject().getPrjId();
            if (StringUtils.isBlank(projectId) || currentUser.getProjectMap() == null || currentUser.getProjectMap().size() <= 0) {
                returnJSON.put(JSON_RESULT, FAILED);
                returnJSON.put(JSON_MSG, "查询当前登录人所属项目失败");
                return returnJSON;
            }

            // 上述check都没有错误的场合
            if (returnJSON.get(JSON_RESULT) == null) {
                returnJSON.put(JSON_RESULT, SUCCESS);
            }

        } catch (Exception ex){
            returnJSON.put(JSON_RESULT, FAILED);
            returnJSON.put(JSON_MSG, ex.getMessage());
        }
        return returnJSON;
    }
    /**
     * 导出
     * @param colName
     * @param colValue
     * @param quickSearch
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/exportOrder")
    public String exportOrder(String colName, String colValue, String quickSearch, String exportType,String searchCondition) throws Exception {
        logger.info("系统订单信息导出：/order/exportOrder");
        try {
            // 查询条件
            Map param = new HashMap<String,Object>();

            // 下面代码放到private方法中时，会获取到上一个请求中的currentUser，所以先移到这里。
            Map<String, Object> returnMap = new HashMap<String, Object>();
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null) {
                logger.error("系统订单信息导出：/order/exportOrder,查询当前登录人失败");
                return null;
            }

            String prjId = currentUser.getCurrentProject().getPrjId();
            param.put("prjId",prjId);

            if (StringUtils.isNotBlank(quickSearch)) {
                param.put("searchCondition",URLDecoder.decode(quickSearch, "UTF-8"));
            }

            colName= URLDecoder.decode(colName, "UTF-8");
            colValue= URLDecoder.decode(colValue, "UTF-8");
            // 高级查询检索条件
            searchCondition = URLDecoder.decode(searchCondition, "UTF-8");
            // json转换成为ＶＯ
            FormVO formVO = JSONObject.parseObject(searchCondition, FormVO.class);
            if (formVO != null) {
                // 标记
                if (StringUtils.isNotBlank(formVO.getStamp())) {
                    param.put("stamp",formVO.getStamp());
                }

                // 售后
                if (StringUtils.isNotBlank(formVO.getCustomerService()) && "1".equals(formVO.getCustomerService())) {
                    param.put("event","1");
                }
                if (StringUtils.isNotBlank(formVO.getCustomerService()) && "0".equals(formVO.getCustomerService())) {
                    param.put("event","0");
                }
                // 合并
                if (StringUtils.isNotBlank(formVO.getCombStus())) {
                    param.put("combStus",formVO.getCombStus());
                }

                // 验视？
                if (StringUtils.isNotBlank(formVO.getHasInspected())) {
                    param.put("hasInspected",formVO.getHasInspected());
                }
                // 平台交易号
                if (StringUtils.isNotBlank(formVO.getTradeCode())) {
                    param.put("tradeCode",formVO.getTradeCode());
                }

                // 订单编号
                if (StringUtils.isNotBlank(formVO.getOrderCode())) {
                    param.put("orderCode",formVO.getOrderCode());
                }

                // 店铺
                if (StringUtils.isNotBlank(formVO.getShopId())) {
                    param.put("shopId",formVO.getShopId());
                }

                // 来源
                if (StringUtils.isNotBlank(formVO.getShopType())) {
                    param.put("shopType",formVO.getShopType());
                }

                // 拍单时间 开始日期
                if (StringUtils.isNotBlank(formVO.getOrderTimeS())) {
                    param.put("orderTimeS",formVO.getOrderTimeS());
                }

                // 付款时间 开始日期
                if (StringUtils.isNotBlank(formVO.getPaidDateS())) {
                    param.put("paidDateS",formVO.getPaidDateS());
                }

                // 发货时间 开始日期
                if (StringUtils.isNotBlank(formVO.getShipTimeS())) {
                    param.put("shipTimeS",formVO.getShipTimeS());
                }

                // 完成时间 开始日期
                if (StringUtils.isNotBlank(formVO.getFinishTimeS())) {
                    param.put("finishTimeS",formVO.getFinishTimeS());
                }

                // 拍单时间 截止日期
                if (StringUtils.isNotBlank(formVO.getOrderTimeE())) {
                    param.put("orderTimeE",formVO.getOrderTimeE());
                }

                // 付款时间 截止日期
                if (StringUtils.isNotBlank(formVO.getPaidDateE())) {
                    param.put("paidDateE",formVO.getPaidDateE());
                }

                // 发货时间 截止日期
                if (StringUtils.isNotBlank(formVO.getShipTimeE())) {
                    param.put("shipTimeE",formVO.getShipTimeE());
                }

                // 完成时间 截止日期
                if (StringUtils.isNotBlank(formVO.getFinishTimeE())) {
                    param.put("finishTimeE",formVO.getFinishTimeE());
                }

                // 规格商家编码
                if (StringUtils.isNotBlank(formVO.getSkuCode())) {
                    param.put("skuCode",formVO.getSkuCode());
                }

                // 顾客ID
                if (StringUtils.isNotBlank(formVO.getBuyerNick())) {
                    param.put("buyerNick",formVO.getBuyerNick());
                }

                // 平台商品编码
                if (StringUtils.isNotBlank(formVO.getEcItemCode())) {
                    param.put("ecItemCode",formVO.getEcItemCode());
                }

                // 平台规格编码
                if (StringUtils.isNotBlank(formVO.getEcSkuCode())) {
                    param.put("ecSkuCode",formVO.getEcSkuCode());
                }

                // 问题单类型
                if (StringUtils.isNotBlank(formVO.getErrorType())) {
                    if (formVO.getErrorType().contains("isOrderError")) {
                        param.put("isOrderError","y");
                    }

                    if (formVO.getErrorType().contains("isGoodsError")) {
                        param.put("isGoodsError","y");
                    }

                    if (formVO.getErrorType().contains("isLgstError")) {
                        param.put("isLgstError","y");
                    }

                    if (formVO.getErrorType().contains("isStockError")) {
                        param.put("isStockError","y");
                    }
                }
            }

            if ("商品信息".equals(exportType)){
                List<OrderExportVO> orderExportList = orderService.exportSku(param);
                exportExcelFile(orderExportList,colName,colValue,"系统订单导出");
            }

        } catch (Exception ex){
            ex.printStackTrace();
            logger.error(ex.getMessage());
        }
        return null;
    }
    
    @ResponseBody
    @RequestMapping(value = "/importFile")
    public Map<String, Object> importFile(String json, String mapAdjustSku) throws Exception {
    	try {
	        logger.info(String.format("导入系统订单：/order/importFile:JSON:"+json));
	
	        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
	        Map<String, String> enterpriseMap = currentUser.getEnterpriseMap();
	        String entId = "";
	//        String entName = "";
	        for (Map.Entry<String, String> entry : enterpriseMap.entrySet()) {
	            entId = entry.getKey();
	//            entName = entry.getValue();
	        }
	
	        String prjId = currentUser.getCurrentProject().getPrjId();
	
	        Map<String,String> map = new HashMap<String,String>();
	        map.put("json",json);
	        map.put("prjId",prjId);
	        map.put("entId",entId);
	        map.put("mapAdjustSku",mapAdjustSku);
	        logger.info(String.format("导入系统订单：/order/importFile:prjId:"+prjId+" entId:"+entId+" JSON:"+json));
	        
	        return orderService.importCheck(map);
    	} catch (Exception ex) {
            logger.error(ex.getMessage());
            Map<String, Object> returnMap = new HashMap<String, Object>();
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, "系统错误");
            return returnMap;
        }
    }
    
}
